The scope of compiler optimizations is often limited by control flow, which prevents optimizations across basic block boundaries. Code duplication can solve this problem by extending basic block sizes, thus enabling subsequent optimizations. Code duplication is a compiler optimization that removes code after control merges and copies the removed code into predecessor blocks. Code duplication enables a compiler to specialize the duplicated code relative to the types and values used in predecessor branches. Code specialization can enable other optimizations. However, discovering optimization opportunities after code duplication is a non-trivial task. In addition, duplicating code for every optimization opportunity may result in excessive code growth.
Without global knowledge about the impact of duplication, a compiler may perform unnecessary (in teams of optimization potential) or even harmful (in terms of code size) transformations. Existing solutions primarily fall into two categories: 1) duplication without considering whether the duplication will enable specific optimizations, and 2) while applying a specific optimization, determining if duplication will expose an opportunity. Duplication approaches for very long word processors aims to increase basic blocks via tail duplication to enable the compiler to perform better instruction selection and scheduling. Duplication may be used to perform complete partial redundancy elimination, or to optimize away conditional and unconditional branches.